package com.gage.commons.utils;

import java.util.Collection;
import java.util.Random;
import java.util.UUID;

/**
 * 继承自Spring util的工具类，减少jar依赖
 * @author L.cm
 */
public class StringUtils extends org.springframework.util.StringUtils {
    /**
     * Check whether the given {@code CharSequence} contains actual <em>text</em>.
     * <p>More specifically, this method returns {@code true} if the
     * {@code CharSequence} is not {@code null}, its length is greater than
     * 0, and it contains at least one non-whitespace character.
     * <p><pre class="code">
     * StringUtils.isBlank(null) = true
     * StringUtils.isBlank("") = true
     * StringUtils.isBlank(" ") = true
     * StringUtils.isBlank("12345") = false
     * StringUtils.isBlank(" 12345 ") = false
     * </pre>
     * @param str the {@code CharSequence} to check (may be {@code null})
     * @return {@code true} if the {@code CharSequence} is not {@code null},
     * its length is greater than 0, and it does not contain whitespace only
     * @see Character#isWhitespace
     */
    public static boolean isBlank(final CharSequence cs) {
        return !StringUtils.isNotBlank(cs);
    }

	/**
	 * 获取名称后缀
	 * @param name
	 * @return
	 */
	public static String getExt(String name){
		if(name == null || "".equals(name) || !name.contains("."))
			return "";
		return name.substring(name.lastIndexOf(".")+1);
	}
	/**
	 * 获取指定位数的随机数
	 * @param num
	 * @return
	 */
	public static String getRandom(int num){
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		for(int i = 0;i < num; i++){
			sb.append(String.valueOf(random.nextInt(10)));
		}
		return sb.toString();
	}
    /**
     * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
     *
     * <pre>
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * </pre>
     *
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is
     *  not empty and not null and not whitespace
     * @see Character#isWhitespace
     */
    public static boolean isNotBlank(final CharSequence cs) {
        return StringUtils.hasText(cs);
    }
    
    /**
     * Convert a {@code Collection} into a delimited {@code String} (e.g. CSV).
     * <p>Useful for {@code toString()} implementations.
     * @param coll the {@code Collection} to convert
     * @param delim the delimiter to use (typically a ",")
     * @return the delimited {@code String}
     */
    public static String join(Collection<?> coll, String delim) {
        return StringUtils.collectionToDelimitedString(coll, delim);
    }
    
    /**
     * Convert a {@code String} array into a delimited {@code String} (e.g. CSV).
     * <p>Useful for {@code toString()} implementations.
     * @param arr the array to display
     * @param delim the delimiter to use (typically a ",")
     * @return the delimited {@code String}
     */
    public static String join(Object[] arr, String delim) {
        return StringUtils.arrayToDelimitedString(arr, delim);
    }
    
    /**
     * 生成uuid
     * @return UUID
     */
    public static String getUUId() {
        return UUID.randomUUID().toString();
    }
    /**
	 * 验证字符串时候为空
	 * 
	 * @author gage
	 * @creationDate. 2012-12-3 下午04:47:52
	 * @param str
	 *            字符串
	 * @return boolean
	 */
	public static boolean isEmpty(Object str) {
		if (str == null) {
			return true;
		}
		if ("".equals(str.toString().trim())) {
			return true;
		}
		if ("null".equals(str.toString().trim().toLowerCase())) {
			return true;
		}
		return false;
	}
	/**
	 * 验证字符串非空
	 * 
	 * @author gage
	 * @creationDate. 2012-12-3 下午04:48:16
	 * @param str
	 *            字符串
	 * @return boolean
	 */
	public static boolean isNotEmpty(Object str) {
		if (str == null) {
			return false;
		}
		if ("".equals(str.toString().trim())) {
			return false;
		}
		if ("null".equals(str.toString().trim().toLowerCase())) {
			return false;
		}
		return true;
	}

	/**
	 * 获得带中文的字符串长度
	 * 
	 * @author gage
	 * @creationDate. 2012-11-2 上午11:36:30
	 * @param str
	 *            字符串
	 * @return 字符串长度
	 */
	public static long getChineseTextLen(String str) {
		if (isEmpty(str))
			return 0;
		return str.replaceAll("[^\\x00-\\xff]", "00").length();
	}

	/**
	 * 截取带中文的文本长度
	 * 
	 * @author gage
	 * @creationDate. 2012-11-2 上午11:37:35
	 * @param str
	 *            字符串
	 * @param len
	 *            长度
	 * @param ext
	 *            截断后添加的标识(一般传省略号)
	 * @return 字符串
	 */
	public static String subChineseText(String str, int len, String ext) {
		if (isEmpty(str))
			return "";
		if (getChineseTextLen(str) <= len)
			return str;
		String reg = "[^\\x00-\\xff]";
		int m = (int) Math.floor(len / 2);
		int length = str.length();
		int subLen = 0;
		for (int i = m; i < length; i++) {
			subLen = str.substring(0, i).replaceAll(reg, "00").length();
			if (subLen >= len) {
				StringBuilder result = new StringBuilder(str.substring(0,
						(subLen > len) ? i - 1 : i));
				if (isNotEmpty(ext)) {
					result.append(ext);
				}
				return result.toString();
			}
		}
		return str;
	}
}
